package com.neu.service;


import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.entity.Disease;
import com.neu.entity.Params;
import com.neu.entity.User;
import com.neu.exception.CustomException;
import com.neu.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    public void add(User user) {
        //判断一下该用户在数据库里有没有
        User dbUser = userMapper.selectByUserName(user.getUsername());
        if(ObjectUtil.isNotEmpty(dbUser)){
            throw new CustomException("用户名已存在");
        }
        //初始化一些页面上没有的信息
        if(ObjectUtil.isEmpty(user.getPassword())){
            user.setPassword("123456");
        }
        if(ObjectUtil.isEmpty(user.getName())){
            user.setName(user.getUsername());
        }
        user.setMember("非会员");
        user.setRole("user");
        userMapper.insert(user);

    }

    public PageInfo<User> findBySearch(Params params) {
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        //接下来的查询会自动按照当前开启的分页设置来查询
        List<User> list = userMapper.findBySearch(params);
        return PageInfo.of(list);
    }

    public void update(User user) {
        userMapper.updateById(user);
    }

    public void delete(Integer id) {
        userMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids){
        for (Integer id : ids) {
            userMapper.deleteById(id);
        }
    }

    public void updateById(User user){
        userMapper.updateById(user);
    }

    public User selectById(Integer id){
        return userMapper.selectById(id);
    }

    public List<User> selectAll(User user){
        return userMapper.selectAll(user);
    }
}
